//
//  HERoomNoticeViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/3/1.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HERoomNoticeViewController.h"
#import "HERoomInfoModel.h"
#import "HERoomNoticeCell.h"

@interface HERoomNoticeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;

@property (nonatomic, strong) HERoomInfoModel *roomModel;

@end

@implementation HERoomNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.zx_navBar.hidden = YES;
    self.view.backgroundColor = CViewBgColor;
    
    [self.view addSubview:self.tableView];
        
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    [self loadData];
}

- (void)loadData {

    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:_roomId forKey:@"id"];
    KWeakSelf
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KroomInfo parameters:dict success:^(id  _Nonnull responseObject) {
       DLog(@"%@", [responseObject mj_JSONString])
       HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
       weakSelf.roomModel = [HERoomInfoModel mj_objectWithKeyValues:response.data];
       
       [self.tableView reloadData];

       [self.tableView.mj_header endRefreshing];

       if (weakSelf.roomModel.notices.count == 0) {
           [self showEmpty];
       }
       
   } failure:^(NSError * _Nonnull error) {
       [self.tableView.mj_header endRefreshing];
   }];
}


- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, 0 , KScreenWidth, KScreenHeight - ZXNavBarHeight - 47) style:UITableViewStylePlain];
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = YES;
            [weakSelf loadData];
        }];
        
        [_tableView registerClass:[HERoomNoticeCell class] forCellReuseIdentifier:@"HERoomNoticeCell"];
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.roomModel.notices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    HERoomNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HERoomNoticeCell"];
    cell.model = _roomModel.notices[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: @"HERoomNoticeCell" cacheByIndexPath:indexPath configuration:^(HERoomNoticeCell *cell) {
        cell.model = self.roomModel.notices[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
 
